package com.typhoon.spring_boot_demo1.proccessor;

import com.typhoon.spring_boot_demo1.dao.UserDao;
import com.typhoon.spring_boot_demo1.exception.UserException;
import com.typhoon.spring_boot_demo1.request.UserOptContext;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class AbstractUserOperation implements IOperation <UserOptContext> {

    @Autowired
    protected UserDao userDao;

    @Override
    public void operate(UserOptContext context) throws UserException {
        checkParameter(context);
        if(hasPermission(context)) {
            execute(context);
        } else {
            notPermissionExecute(context);
        }

    }

    /**
     * 校验操作权限和数据权限
     *
     * @param context
     * @return
     * @throws UserException
     */
    public boolean hasPermission(UserOptContext context) throws UserException {
        return true;
    }

    /**
     * 具体执行操作，交给子类实现
     *
     * @param context
     * @throws UserException
     */
    public abstract void execute(UserOptContext context) throws UserException;

    /**
     * 没有权限执行的操作
     *
     * @param context
     * @throws UserException
     */
    public void notPermissionExecute(UserOptContext context) throws UserException {
        throw new UserException("没有权限操作");
    }



    /**
     * 参数校验，交给子类实现差异化
     *
     * @param context
     * @throws UserException
     */
    public abstract void checkParameter(UserOptContext context) throws UserException ;
}
